package com.learn.project2.service.admin.history;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketSearchHistory;
import com.learn.project2.bean.MarketSearchHistoryExample;
import com.learn.project2.mapper.MarketSearchHistoryMapper;
import com.learn.project2.model.bo.admin.user.UserListBO;
import com.learn.project2.model.vo.admin.history.HistoryListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author wxy
 * @date 2022-01-08 11:52
 * @version 1.0
 */
@Service
//@CacheConfig(cacheNames = "marketHistory")
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    MarketSearchHistoryMapper historyMapper;
    @Override
    public HistoryListVO selectHistory(String keyword, Integer userId, UserListBO userListBO) {
        Integer limit = userListBO.getLimit();
        Integer page = userListBO.getPage();
        String sort = userListBO.getSort();
        String order = userListBO.getOrder();
        //使用分页插件
        PageHelper.startPage(page,limit);
        MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = historyExample.createCriteria();
        //keyword 的模糊查询，userId的精确查询
        if (keyword != null){
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        criteria.andDeletedEqualTo(false);
        historyExample.setOrderByClause(sort+" "+order);
        List<MarketSearchHistory> list = historyMapper.selectByExample(historyExample);
        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(list);

        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return HistoryListVO.data(total,pages,userListBO.getLimit(),userListBO.getPage(),list);
    }
}
